package com.guragura.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector3;
import com.guragura.Guragura;
import com.guragura.util.GuraScreenInputListener;
import com.guragura.util.ResInitializable;

/**
 * Base class for Guragura's screen  
 * @author hartono
 *
 */
public abstract class GuraScreen implements Screen, ResInitializable{

	public final static int SCREEN_RUNNING = 0;
	public final static int SCREEN_LOADING = 1;
	public final static int SCREEN_PAUSE = 2;
	
	public AssetManager assetManager;
	public BitmapFont font;
	public Guragura guragura;
	public SpriteBatch batch;
	public OrthographicCamera camera;
	public GuraScreenInputListener inputListener;
	public int state = SCREEN_LOADING;
	
	public boolean resourcesLoaded = false;
	
	public GuraScreen() {
		
	}
	
	/** 
	 * @param guragura
	 */
	public GuraScreen(Guragura guragura){
		this.guragura = guragura;
		batch = this.guragura.batch;			
		assetManager = this.guragura.assetManager;
		font = this.guragura.font;
		
//		camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		camera = new OrthographicCamera(320, 480);
//		camera.position.set(Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()/2,0);	
		camera.position.set(160,240,0);
		initScreen();
		
	}
	
	/**
	 * This method should be used for initialize object that not use resources like texture. 
	 */
	public abstract void initScreen();
	
	/* (non-Javadoc)
	 * @see com.badlogic.gdx.Screen#render(float)
	 * 
	 * method ini akan merender sesuai state screen ini
	 */
	@Override
	public void render(float arg0) {
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		batch.setProjectionMatrix(camera.combined);									
		
		camera.update();	
		if (state == SCREEN_RUNNING){
			onScreenRunning();
		}else if (state == SCREEN_PAUSE){
			onScreenPause();
		}else if (state == SCREEN_LOADING){
			onScreenLoading();
		}		
	}	
	
	@Override
	public void show() {
		if (inputListener!=null){
			Gdx.input.setInputProcessor(inputListener);
		}else{
			Gdx.input.setInputProcessor(null);
		}
	}
	
	@Override
	public void hide() {
		
	}
	
	@Override
	public void resume() {
		
	}
	
	@Override
	public void pause() {
		
	}
	
	@Override
	public void dispose() {	
		
	}
	
	@Override
	public void resize(int arg0, int arg1) {
		// TODO Auto-generated method stub
//		camera = new OrthographicCamera(arg0, arg1);
//		camera = new OrthographicCamera(guragura.screenWidth, guragura.screenHeight);
//		camera.position.set(guragura.screenWidth/2, guragura.screenHeight/2, 0);
//		camera.position.set(arg0/2, arg1/2,0);
		
	}
	
	
	/**
	 * akan dirender ketika {@link GuraScreen#state} adalah {@link GuraScreen#SCREEN_RUNNING}
	 */
	public abstract void onScreenRunning();
	/**
	 * akan dirender ketika {@link GuraScreen#state} adalah {@link GuraScreen#SCREEN_PAUSE}
	 */
	public abstract void onScreenPause();
	/**
	 * akan dirender ketika {@link GuraScreen#state} adalah {@link GuraScreen#SCREEN_LOADING}
	 */
	public abstract void onScreenLoading();
	
	
	/**
	 * dipanggil untuk keluar dari screen ini
	 */
	public void exitScreen(){
		hide();
		guragura.popScreen();		
	}
		
}
